﻿//工厂那边通过日期查询订单
package com.webbei.Backstage.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webbei.Backstage.service.BackstateSer;
import com.webbei.pojo.Labelcost;
import com.webbei.pojo.Managers;
import com.webbei.pojo.Orders;
import com.webbei.pojo.Trade;
import com.webbei.util.Arithmetic;

public class SelectinduscostBydate extends ActionSupport{
      private String startTime;
      private String endTime;
      private String orstate;
      private String mark;
      private Integer productId;
      private String tradeId;
      private BackstateSer bao;  
      private List<Orders> orderlist;
      private List<Trade> trades;
      private  Double alltotal;
      private Double sale;
      private int page;
      private static final int MAX = 50;
      private int pagecount;
      private int total;
      private String type="info";
    public String execute(){
    	alltotal=0.0;
    	sale=0.0;
    	 System.out.println("SelectinduscostBydate....");
		   trades = new ArrayList<Trade>();
		Map<String,Object>m = (Map<String, Object>) ServletActionContext.getRequest().getSession().getAttribute("query");
		System.out.println(m);
		
		if(m!=null){
			m.put("orstate", orstate);
		       total=bao.getOrdercount(m);
			   pagecount = (total% MAX) == 0 ? (total/MAX): (total/MAX+ 1);
			   System.out.println("pagecount:"+pagecount);
				if (page> pagecount) {
					page=pagecount;
				}
				if (page< 1) {
					page = 1;
				}
				int start = (page - 1) * MAX;
				m.put("start", start);
				m.put("maxpage", MAX);
				 orderlist = bao.getorderByday(m);
				 System.out.println("size*********"+orderlist.size());
				 int len=orderlist.size();
				 Arithmetic.getKey("webbei"); 
				 for(int i=0;i<len;i++){
					 Orders o = orderlist.get(i);
						List<Orders> orders = bao.getOrdersByTradeId(o.getTradeId());
						int size = orders.size();
						for(int j=0;j<size;j++){
							Orders or = orders.get(j);
							String cdrUrl = or.getCDRurl();
							String mark = or.getMark();
							String sku = or.getSku();
							Integer picId = or.getBackPicId();
							 if(cdrUrl!=null){
								 or.setCDRurl(Arithmetic.getEncString(cdrUrl));
							 }
							 int labelId=-1;
								if(picId!=null){
									labelId=bao.getLabelIdByPicId(picId);
								}
								if(mark!=null&&!("".equals(mark))){
									mark = "%"+mark+"%";
									Map<String,Object>map = new HashMap<String, Object>();
									map.put("mark", mark);
									if(sku!=null&&"".equals(sku)){
										sku="%"+sku+"%";
									}
									map.put("sku", sku);
									labelId = bao.getLableIdByMap(map);
								}
								if(labelId!=0){
									or.setLabelCosts(bao.getLabelCostByLabelId(labelId));
								}
							 
						}
						Trade t = new Trade();
						t.setTradeId(o.getOrderId());
						t.setOrders(orders);
						t.setCount(orders.size());
						trades.add(t);
				 }
				 for(Trade t:trades){
					 sale+=t.getPrice();
					 for(Orders o:t.getOrders()){
						 if(o.getLabelCosts()!=null){
						 for(Labelcost l:o.getLabelCosts()){
							 if(l.getPrice()!=null){
							 alltotal+=l.getPrice();
							 }
						 }
						 }
					 }
				 }
		}
  	   return SUCCESS;
       }

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getOrstate() {
		return orstate;
	}

	public void setOrstate(String orstate) {
		this.orstate = orstate;
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public String getTradeId() {
		return tradeId;
	}

	public void setTradeId(String tradeId) {
		this.tradeId = tradeId;
	}

	public List<Orders> getOrderlist() {
		return orderlist;
	}

	public void setOrderlist(List<Orders> orderlist) {
		this.orderlist = orderlist;
	}

	public Double getAlltotal() {
		return alltotal;
	}

	public void setAlltotal(Double alltotal) {
		this.alltotal = alltotal;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPagecount() {
		return pagecount;
	}

	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setBao(BackstateSer bao) {
		this.bao = bao;
	}

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public List<Trade> getTrades() {
		return trades;
	}

	public void setTrades(List<Trade> trades) {
		this.trades = trades;
	}

	public Double getSale() {
		return sale;
	}

	public void setSale(Double sale) {
		this.sale = sale;
	}

	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
}
